<?php


namespace app\mingteng\controller;
use think\AjaxPage;
use think\Db;

class Voucher extends Base
{
    public $listRows = 20;
    public function index(){
        return $this->fetch();
    }

    public function ajax_index(){
        $params = input('post.');
        $page = input('page', '1');

        $where = array();
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $where['voucherName'] = array('like', '%'.$params['keywords'].'%');
        }
        $count = Db::name('voucher')->where($where)->count();
        $list = Db::name('voucher')->where($where)->page($page, $this->listRows)->select();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['addTime'] = date('Y-m-d H:i',$v['addTime']);
            }
        }
        $this->assign('list', $list);

        $ajaxPage = new AjaxPage($count, $this->listRows);
        $show = $ajaxPage->show();
        $this->assign('page', $show);

        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }

    public function doAdd(){
        $params = input('post.');

        if (empty($params['voucherName'])) {
            return json(array('code' => 500, 'msg' => '请输入抵扣券名称'));
        }
        if (empty($params['voucherAmount'])) {
            return json(array('code' => 500, 'msg' => '请输入抵扣券金额'));
        }
        if (empty($params['voucherType'])) {
            return json(array('code' => 500, 'msg' => '请选择使用场景'));
        }

        $catch = array(
            'voucherName' => $params['voucherName'],
            'voucherAmount' => $params['voucherAmount'],
            'useRule' => !empty($params['useRule']) ? $params['useRule'] : false,
            'voucherType' => $params['voucherType'],
            'addTime' => time(),
        );
        $couponId = Db::name('voucher')->insertGetId($catch);
        return json(array('code' => 200, 'msg' => '添加成功'));
    }

    public function edit(){
        $voucherId = input('voucherId');

        $voucher= Db::name('voucher')->where('voucherId', '=', $voucherId)->find();
        $this->assign('voucher', $voucher);

        return $this->fetch();
    }

    public function doEdit(){
        $params = input('post.');

        if (empty($params['voucherName'])) {
            return json(array('code' => 500, 'msg' => '请输入抵扣券名称'));
        }
        if (empty($params['voucherAmount'])) {
            return json(array('code' => 500, 'msg' => '请输入抵扣券金额'));
        }
        if (empty($params['voucherType'])) {
            return json(array('code' => 500, 'msg' => '请选择使用场景'));
        }

        $catch = array(
            'voucherName' => $params['voucherName'],
            'voucherAmount' => $params['voucherAmount'],
            'useRule' => !empty($params['useRule']) ? $params['useRule'] : false,
            'voucherType' => $params['voucherType'],
            'addTime' => time(),
        );
        Db::name('voucher')->where('voucherId', '=', $params['voucherId'])->save($catch);
        return json(array('code' => 200, 'msg' => '编辑成功'));
    }

    public function del(){
        $del_id = input('del_id');

        Db::name('voucher')->where('voucherId', '=', $del_id)->delete();
        return json(array('code' => 200, 'msg' => '操作成功'));
    }
    //==================================================抵扣券使用限制设置=================================================
    /**
     * 设置页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function set_index(){

        $data = Db::name('voucher_limitations_set')->where('id',1)->find();

        $this->assign('num',$data['num']?$data['num']:'');//当前配置项
        return $this->fetch('set_index');
    }

    /**
     * 编辑
     */
    public function set_add(){
        $param = input('post.');

        $one = Db::name('voucher_limitations_set')->where('id',1)->find();
        if($one){
            $data = Db::name('voucher_limitations_set')->where('id',1)->update(['num'=>$param['num']]);
        }else{
            $data = Db::name('voucher_limitations_set')->insert(['num'=>$param['num']]);
        }

        if($data){
            $this->success("操作成功",U('Voucher/set_index'));
        }else{
            $this->error("操作失败",U('Voucher/set_index'));
        }

    }
}